package com.itheima.jobs;

import com.itheima.constant.RedisConstant;
import com.itheima.utils.QiniuUtils;
import com.qiniu.common.QiniuException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import redis.clients.jedis.JedisPool;

import java.util.Set;

@Component
public class ClearImg {

    @Autowired
    JedisPool jedisPool;

    public void clearImg() throws QiniuException {
        Set<String> imgNameSet = jedisPool.getResource().sdiff(RedisConstant.SETMEAL_PIC_RESOURCES, RedisConstant.SETMEAL_PIC_DB_RESOURCES);
        if (imgNameSet.size()>0) {
            //注意set转数组的方式
            String[] imgNameArray = imgNameSet.toArray(new String[imgNameSet.size()]);

            //删除云端垃圾图片
            QiniuUtils.delFiles(imgNameArray);

            //删除redis中的垃圾缓存
            jedisPool.getResource().zrem(RedisConstant.SETMEAL_PIC_RESOURCES, imgNameArray);
        }

    }
}
